namespace Product.Api.Types;

[ExtendObjectType("Query")]
public class ProductResolver
{
    public IEnumerable<Product> GetTopProducts(
        int first,
        [Service] ProductRepository repository) =>
        repository.GetTopProducts(first);

    public Product GetProductById(
        int upc,
        [Service] ProductRepository repository) =>
        repository.GetProduct(upc);

    public IEnumerable<Product> GetProductsById(
        int[] upc,
        [Service] ProductRepository repository)
    {
        foreach (var id in upc)
        {
            yield return repository.GetProduct(id);
        }
    }
}

public record Product(int Upc, string Name, int Price, int Weight);

public class ProductRepository
{
    private readonly Dictionary<int, Product> _products;

    public ProductRepository()
    {
        _products = new Product[]
        {
            new Product(1, "Table", 899, 100),
            new Product(2, "Couch", 1299, 1000),
            new Product(3, "Chair", 54, 50)
        }.ToDictionary(t => t.Upc);
    }

    public IEnumerable<Product> GetTopProducts(int first) =>
        _products.Values.OrderBy(t => t.Upc).Take(first);

    public Product GetProduct(int upc) => _products[upc];
}
